package com.ydm.myapp001.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ydm.myapp001.R;
import com.ydm.myapp001.bean.OneGroupBean;

import java.util.ArrayList;

public class RecycleViewFenZuAdapter  extends RecyclerView.Adapter <RecyclerView.ViewHolder> {
    private Context mContext;
    private ArrayList<OneGroupBean> listItems; //二维数组
    private static final int TYPE_CONTENT = 1;
    private static final int TYPE_Title = 0;

    public RecycleViewFenZuAdapter(Context mContext, ArrayList<OneGroupBean> listItems) {
        this.mContext = mContext;
        this.listItems = listItems;
    }

    /**
     * 用于创建ViewHolder实例的，并把加载出来的布局传入到构造函数当中，最后将viewholder的实例返回
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 不需要检查是否复用，因为只要进入此方法，必然没有复用
        // 因为RecyclerView 通过Holder检查复用
        View view = null;
        RecyclerView.ViewHolder holder = null;

        switch (viewType) {
            case TYPE_Title:
                view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_fenzu_title, parent, false);
                holder = new TitleItemViewHolder(view);
                break;
            case TYPE_CONTENT:
                view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_fenzu_content, parent, false);
                holder = new NormalItemViewHolder(view);
                break;
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int count = 0;
        for (int i = 0; i < listItems.size(); i++) {
            OneGroupBean bean = listItems.get(i);
            ArrayList<String> dataList = bean.getDataList();
            if (position == count) {
                TitleItemViewHolder vGroup = (TitleItemViewHolder) holder;
                vGroup.textView.setText(bean.getGroupTitle());
                return;
            }
            else {
                count++;
                for (int j = 0; j < dataList.size(); j++) {
                    if (position == count) {
                        NormalItemViewHolder vGroup = (NormalItemViewHolder) holder;
                        vGroup.textView.setText(dataList.get(j));
                        return;
                    }
                    else {
                        count++;
                    }
                }
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        int count = 0;
        for (int i = 0; i < listItems.size(); i++) {
            OneGroupBean bean = listItems.get(i);
            ArrayList<String> dataList = bean.getDataList();
            if (position == count) {
                return TYPE_Title;
            }
            else {
                count++;
                for (int j = 0; j < dataList.size(); j++) {
                    if (position == count) {
                        return TYPE_CONTENT;
                    }
                    else {
                        count++;
                    }
                }
            }

        }

        return TYPE_CONTENT;
    }

    @Override
    public int getItemCount() {
        int count = listItems.size();
        for (int i = 0; i < listItems.size(); i++) {
            OneGroupBean bean = listItems.get(i);
            count += bean.getDataList().size();
        }
        return count; //所有组的item个数 + 组标题数
    }


    class NormalItemViewHolder extends RecyclerView.ViewHolder {

        TextView textView;

        public NormalItemViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView;
        }
    }

    class TitleItemViewHolder extends RecyclerView.ViewHolder {

        TextView textView;

        public TitleItemViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView;
        }
    }
}
